<template>
  <div>
    <div class="tools">
      <div>
        <el-input prefix-icon="el-icon-search" style="width: 200px;margin-right: 10px;" size="small" v-model="input"
                  placeholder="请输入内容"></el-input>
        <el-button icon="el-icon-search" type="primary" size="small">搜索</el-button>
      </div>
      <div>
        <el-button @click="add" icon="el-icon-plus" type="primary" size="small">添加</el-button>
      </div>
    </div>
    <el-table
        :data="typeData"
        style="width: 100%"
        :row-class-name="tableRowClassName">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          prop="id"
          label="编号"
          width="100">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
          prop="typename"
          label="宠物名称"
          width="200">
      </el-table-column>
      <el-table-column
          prop="remark"
          label="宠物简介"
          width="200">
      </el-table-column>
      <el-table-column
          prop="img"
          label="宠物图片"
          width="200">
      </el-table-column>
      <el-table-column
          prop="petType"
          label="宠物类型"
          width="200">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.petType}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="edit(scope.row)">编辑
          </el-button>
          <el-button
              size="mini"
              type="danger"
              @click="deleteData(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
    <dictionary-oper ref="operation"></dictionary-oper>
  </div>
</template>

<script>
import DictionaryOper from "@/view/dic/modules/DictionaryOper";

export default {
  name: "Pet",
  components: {DictionaryOper},
  data() {
    return {
      input: '',
      //图片回显路径
      imgUrl: '',
      suffixDomain: "",
      typeData: [],
      total: 0,
      pageSize: 0,
      pageSizes: [10, 20, 30, 40],
      currentPage: 1,
    };
  },
  created() {
    this.suffixDomain = this.imgDomain;
  },
  mounted() {
    this.initData();
  },
  methods: {
    add() {
      this.$refs.operation.dialogVisible = true;
      this.$refs.operation.title = "添加";
      this.$refs.operation.operationType = 0;//0添加 1编辑
      this.$refs.operation.clearObj(this.$refs.operation.img)
    },
    edit(data) {
      this.$refs.operation.dialogVisible = true;
      this.$refs.operation.title = "编辑";
      this.$refs.operation.operationType = 1;//0添加 1编辑
      this.$refs.operation.type = data;
    },
    initData() {
      this.getRequest("/pet/type/", {pageSize: 10, pageNo: 1}).then(resp => {
        if (resp.code === 200) {
          this.typeData = resp.data.records;
          this.total = resp.data.total;
          this.pageSize = resp.data.size;
          this.currentPage = resp.data.current;
        }
      });
    },
    deleteData(index, data) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest("/pet/type-dictionary/" + data.id).then(resp => {
          if (resp.code === 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.initData();
          } else {
            this.$message({
              type: "error",
              message: "删除失败！"
            })
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    updateFlag(row) {
      this.$confirm('此操作将修改该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let tempFlag = 0;
        if (row.flag === 0) {
          tempFlag = 1;
        }
        row.flag = tempFlag;
        this.putRequest("/swiper/img/updateSwiper", row).then(resp => {
          if (resp.code === 200) {
            this.$message({
              type: 'success',
              message: '修改成功!'
            });
            this.initData();
          } else {
            this.$message({
              type: 'error',
              message: '修改失败!'
            });
          }
        });

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    tableRowClassName({row, rowIndex}) {
      if (rowIndex === 1) {
        return 'warning-row';
      } else if (rowIndex === 3) {
        return 'success-row';
      }
      return '';
    },
    handleSizeChange() {

    },
    handleCurrentChange() {

    },
  },
};
</script>

<style scoped>
.tools {
  display: flex;
  justify-content: space-between;
}

.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}

.column {
  display: flex;
  align-items: center;
  margin-top: 15px;
}

.operation-div {
  display: flex;
  justify-content: space-around;
}

.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
</style>
